package top.defaults.colorpicker;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import org.jetbrains.annotations.Nullable;

/**
 * BrightnessSliderView
 *
 * @author jjy
 * @since 2021-05-10
 */
public class BrightnessSliderView extends ColorSliderView implements Component.DrawTask {

    private int width;
    private int height;

    /**
     * 构造函数
     *
     * @param context context对象
     */
    public BrightnessSliderView(Context context) {
        super(context);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     */
    public BrightnessSliderView(Context context, @Nullable AttrSet attrs) {
        super(context, attrs);
    }

    /**
     * 构造函数
     *
     * @param context context对象
     * @param attrs 属性值
     * @param defStyleAttr 样式名称
     */
    public BrightnessSliderView(Context context, @Nullable AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void invalidate() {
        super.invalidate();
        this.addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        width = getWidth();
        height = getHeight();
        onSizeChanged(width, height);
        super.onDraw(component, canvas);
    }

    @Override
    protected float resolveValue(int color) {
        int[] rgbColor = ColorConverUtils.colorToRgb(color);
        double[] hsvColor = ColorConverUtils.rgbToHsv(rgbColor[0], rgbColor[1], rgbColor[2]);
        return (float) hsvColor[2];
    }

    /**
     * 画笔对象
     *
     * @param colorPaint 画笔对象
     */
    protected void configurePaint(Paint colorPaint) {
        int[] baseRgbColors = ColorConverUtils.colorToRgb(baseColor);
        double[] hsv = ColorConverUtils.rgbToHsv(baseRgbColors[0], baseRgbColors[1], baseRgbColors[2]);
        int[] startColors = ColorConverUtils.hsvToRgb((float) hsv[0], (float) hsv[1], 0);
        int endColor = baseColor;
        Color startColor = new Color(Color.rgb((int) startColors[0], (int) startColors[1], (int) startColors[2]));
        Color[] colors = new Color[]{startColor, new Color(endColor)};
        Point[] pointArrays = new Point[]{new Point(0, 0),
            new Point(width, height)};
        Shader shader = new LinearShader(pointArrays,
            null, colors, Shader.TileMode.CLAMP_TILEMODE);
        colorPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
    }

    /**
     * 转变颜色
     *
     * @return 返回颜色值
     */
    protected int assembleColor() {
        int[] rgbColor = ColorConverUtils.colorToRgb(baseColor);
        double[] hsvColor = ColorConverUtils.rgbToHsv(rgbColor[0], rgbColor[1], rgbColor[2]);
        hsvColor[2] = currentValue;
        return Color.rgb((int) hsvColor[0], (int) hsvColor[1], (int) hsvColor[2]);
    }
}
